

use "$dir\data\interim\cz_contemp_tech.dta", clear
	// Merge Instrument
	merge 1:1 czone year using "$dir\data\interim\cz_tech_instrument_ind_2000.dta" 
	drop if _merge==2
	drop _merge
	
		merge 1:1 czone year using "$dir\data\interim\cz_tech_instrument_ind_1970.dta" 
	drop if _merge==2
	drop _merge
	
	
	// Merge outcomes
	gen OUTCOMES = "/////////////////"
	merge 1:m czone year using "$dir\data\interim\outcome_vars_acs.dta"
	keep if _merge ==3
	drop _merge
	
	drop if year ==.

	// Standardize 
	foreach var in  instrument_mean_ind inst_mean_oas_ind  inst_any_oas_ind  {
		sum `var'_2000
		gen `var'_2000_std = (`var'_2000-r(mean))/r(sd)

	}
	sum contemp_mean
	gen contemp_mean_std = (contemp_mean-r(mean))/r(sd)
	

	
	
	
	
sum contemp_mean if year == 2007
gen contemp_mean_alt_std = (contemp_mean - r(mean))/r(sd)

sum instrument_mean_ind_2000 if year == 2007
gen inst_mean_ind_std_2000 =   (instrument_mean_ind_2000-r(mean))/r(sd)

sum instrument_mean_ind_1970 if year == 2007
gen inst_mean_ind_std_1970 =   (instrument_mean_ind_1970-r(mean))/r(sd)


*instrument_office_ind_2000 contemp_any_office
sum contemp_any_office if year == 2007
gen contemp_any_office_std = (contemp_any_office - r(mean))/r(sd)

sum instrument_office_ind_2000 if year == 2007
gen inst_office_ind_std_2000 =   (instrument_office_ind_2000-r(mean))/r(sd)


sum instrument_office_ind_1970 if year == 2007
gen inst_office_ind_std_1970 =   (instrument_office_ind_1970-r(mean))/r(sd)


*alternative measure for robustness check: weighted by just the share within OAS rather than all employment
sum contemp_mean_oas if year == 2007
gen contemp_mean_oas_std = (contemp_mean_oas - r(mean))/r(sd)

*and the corresponding instrument
sum inst_mean_oas_ind_2000 if year == 2007
gen inst_mean_oas_ind_std_2000 = (inst_mean_oas_ind_2000 - r(mean))/r(sd)

*/

*want to merge in the constant demo wages


 merge 1:1 year czone using "$dir\data\interim\demoadj_outcomes.dta"
drop _merge






tab year, gen(yr2_)
tab czone, gen(cz2_)
drop yr2_1
drop cz2_1

gen epop100 = 100*czone_emp_pop
gen flfp100=100*flfp
gen mlfp100=100*mlfp	

global occupations oas non_oas blue_collar pink_collar white_m_collar white_f_collar white_collar ///
const heal mgmt bus_fin_op pc_math arch_eng phys_scl_scnc ///
com_scl_srvc legal education arts_dsgn hlth_spprt protection food_prep bldng_mntnce ///
prsnal_care sales farm ins_mntnc_rpr production transport  
	
foreach occ of global occupations  {
	rename cz_`occ'_pop_pct `occ'_pop_pct
	rename cz_college_`occ'_pct `occ'_college_pct 
	rename lwg_annual_`occ'_r `occ'_cz_lwg_annual_r 
	rename lwg_yr_col`occ'_r  `occ'_cz_lwg_yr_col_r  
	rename lwg_yr_nocol`occ'_r `occ'_cz_lwg_yr_nocol_r 
	rename dlwg_annual_`occ'_r `occ'_dlw_annual_r
	}
	
	



gen femcol_epop100 = 100*femcol_pct
gen femnocol_epop100 = 100*femnocol_pct
gen malecol_epop100 = 100*malecol_pct
gen malenocol_epop100 = 100* malenocol_pct

gen nonoas_nocol_epop100 = nonoas_nocol_pct*100
gen oas_nocol_epop100 = oas_nocol_pct*100
gen nonoas_col_epop100 = nonoas_col_pct*100
gen oas_col_epop100 = oas_col_pct*100
	
	gen oas_nc_wage_gap = oas_cz_lwg_annual_r- non_oas_cz_lwg_yr_nocol_r
	gen oas_col_wage_gap = oas_cz_lwg_annual_r -non_oas_cz_lwg_yr_col_r
	gen oas_nc_nc_wage_gap = oas_cz_lwg_yr_nocol_r- non_oas_cz_lwg_yr_nocol_r
	gen oas_nc_col_wage_gap = oas_cz_lwg_yr_nocol_r- non_oas_cz_lwg_yr_col_r
	gen oas_col_nc_wage_gap = oas_cz_lwg_yr_col_r- non_oas_cz_lwg_yr_nocol_r
	gen oas_col_col_wage_gap = oas_cz_lwg_yr_col_r- non_oas_cz_lwg_yr_col_r
	
	




*merge in the non-oas tech

merge 1:1 czone year using "$dir\data\interim\mean_tech_by_cz_non_oas.dta"
drop _merge
ren mean_* mno_*

*merge in the share of employment in non-oas occs in 2000 to construct the share control
merge m:1 czone using "$dir/data/interim/non_oas_share_weight_2000.dta"
drop if _merge ==2
drop _merge
ren occ_share nonoas_share2000


*merge in the share of employment in non-oas occs in 1970 to construct the share control
merge m:1 czone using "$dir/data/interim/non_oas_share_weight_1970.dta"
drop if _merge ==2
drop _merge
ren occ_share nonoas_share1970


sum mno_count_tech if year == 2007
		gen mno_count_tech_std = (mno_count_tech-r(mean))/r(sd)
		
	
sum mno_count_issoftware if year == 2007
		gen mno_count_issoftware_std = (mno_count_issoftware-r(mean))/r(sd)


gen czone_pop1000s = czone_pop/1000
foreach var in czone_sh_manufac czone_sh_services czone_sh_hschool czone_sh_some_college czone_sh_college czone_sh_fem_emp czone_sh_black czone_sh_asian czone_sh_foreign czone_pop czone_pop1000s  {

gen temp = `var' if year == 2007
gen temp2 = `var' if year == 2016
egen temp3 = min(temp), by(czone)
egen temp4 = min(temp2), by(czone)
gen `var'_dif = temp4-temp3

drop temp*
}



gen temp = czone_pop>=1000000 if year == 2007 //czones with >1 mill in 2007
egen lg_cz = max(temp), by(czone)
drop temp



foreach var in   inst_mean_ind_std_1970 inst_mean_ind_std_2000  {

gen temp = `var' if year == 2007
gen temp2 = `var' if year == 2016
egen temp3 = min(temp), by(czone)
egen temp4 = min(temp2), by(czone)
gen `var'_dif = temp4-temp3

drop temp*
}

*robustness 5: drop the top 10% of tech adopting czones in 2007	
sum contemp_mean_oas_std if year==2007, detail
gen topdec_contemp = 0
replace topdec_contemp = 1 if contemp_mean_oas_std> r(p90) & year == 2007

by czone, sort: gen top10_flag = sum(topdec_contemp)	


	*robustness 6: drop the bottom 10% of tech adopting czones in 2007
	
	
sum contemp_mean_oas_std if year==2007, detail
gen bottomdec_contemp = 0
replace bottomdec_contemp = 1 if contemp_mean_oas_std< r(p10) & year == 2007

by czone, sort: gen bottom10_flag = sum(bottomdec_contemp)	


save "$dir\data\cleaned\estimation_data.dta", replace

